$(document).ready(function () {
    var userstring = sessionStorage.getItem('user');
    var user = JSON.parse(userstring);
    var nickname=user.nickname;
    var uid=user.id;
    $('#nickname').text(nickname);
    // 1. 窗体加载后，发送异步请求，获得全部数据
    var likefname;
    select();
    //查询全部选项
    $("#查询全部").on("click", function() {
        location.reload();
    });
    $("#名称查询").on("click", function() {
        likefname=$("[name='搜索文本框']").val();
        select();
    });

    //按照商品类别查找商品
    $(document).on("click", "a[title='查询']", function () {
        ccid = $(this).attr("name");
        select();
    });

    //点击图片跳转到详情
    $(document).on("click", "a[title='图片']", function () {
        var id = $(this).attr("name");
        $.get("view/add&del",{"uid":uid,"fid":id},function () {});
        window.location.href = "http://localhost:8080/detail.html?id=" + id;
    });

    //获得跳转链接中的参数cid
    const urlParams = new URLSearchParams(window.location.search);
    var ccid;
    if(urlParams.get('cid')!=null) {
        ccid=urlParams.get('cid');
        select();
    }

    //获得来自商品详情页搜索框里的值
    if(urlParams.get('fname')!=null) {
        likefname = urlParams.get('fname');
        select();
    }

    var desc;
    var salesVolume;
    var price;
    $("a[title='查询']").on("click",function () {
        ccid=$(this).attr("name");
        select();

    });
    $(".active").on("click",function () {
        desc=$(this).attr("name");
        if(desc==1){
            price=1;
            salesVolume=0;
        }else if(desc==2){
            salesVolume=2;
            price=0;
        }
        select();
    });
    var pageNum;
    function select() {
        pageNum=1;
        selectAll();
    }
    function selectAll() {
        $.get("food/show",{"cid":ccid,
            "salesVolume":salesVolume,
            "price":price,"fname":likefname,
            "pageNum":pageNum,
            "pageSize":10
        },function (rs) {
            var moren = rs.result.list;
            var ps=rs.result.pages;
            pages(ps);
            $("[name='body']").empty();
            for (var i = 0; i < moren.length; i++) {
                $("<li>\n" +
                    "    <a href='#' name='" + moren[i].id + "' title='图片'><img src='" + moren[i].image + "'></a>\n" +
                    "    <h4><a href='#'>" + moren[i].fname + "</a></h4>\n" +
                    "<div class='operate'>"+
                    "<span class='prize'>￥"+moren[i].price+"</span>"+
                    "<span class='unit'>"+moren[i].price+"/500g</span>"+
                    "<a href='#' class='add_goods' title='加入购物车'></a>"+
                    "</div>"+
                    "</li>").appendTo("[name='body']");
            }
        });
    }
    var currentPage = 1; // 当前页
    function pages(totalPages) {
        function createPagination(totalPages, currentPage) {
            const container = document.getElementById('apagination');
            container.innerHTML = ''; // 清空当前内容

            // 创建上一页链接
            if (currentPage > 1) {
                const prev = document.createElement('a');
                prev.href = 'javascript:void(0);';
                prev.textContent = '上一页';
                prev.addEventListener('click', (e) =>{
                    e.preventDefault();
                    changePage(currentPage - 1);
                });
                container.appendChild(prev);
            }

            // 创建页码链接
            for (let i = 1; i <= totalPages; i++) {
                const pageLink = document.createElement('a');
                pageLink.href = 'javascript:void(0);';
                pageLink.textContent = i;
                if (i === currentPage) {
                    pageLink.classList.add('active');
                }
                pageLink.addEventListener('click', (e) => {
                    e.preventDefault();
                    changePage(i);
                });
                container.appendChild(pageLink);
            }

            // 创建下一页链接
            if (currentPage < totalPages) {
                const next = document.createElement('a');
                next.href = 'javascript:void(0);';
                next.textContent = '下一页';
                next.addEventListener('click', (e) =>{
                    e.preventDefault();
                    changePage(currentPage + 1)
                });
                container.appendChild(next);
            }
        }

        function changePage(page) {
            if (page < 1 || page > totalPages) return;
            currentPage = page;
            pageNum = currentPage;
            selectAll();
            createPagination(totalPages, currentPage);
            // 可以在这里添加额外的逻辑，例如加载对应页的内容
            console.log('当前页:', currentPage);
        }

        // 初始化分页
        createPagination(totalPages, currentPage);
    }
});